/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SeqPanel.java
 *
 * Created on Apr 24, 2010, 10:55:03 AM
 */

package thetaeditor;
import javax.swing.ButtonGroup;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 *
 * @author garrett
 */
public class SeqPanel extends javax.swing.JPanel {

    private Sequence sequence;

    /** Creates new form SeqPanel */
    public SeqPanel(ButtonGroup defaultsGroup, Sequence seq) {
        initComponents();

        defaultsGroup.add(defaultRadio);

        if (seq == null)
            sequence = new Sequence();
        else
            sequence = seq;

        if (sequence.name != null) {
            nameField.setText(sequence.name);
        }

        editButton.addActionListener(new EditListener());
    }

    public void setSequenceName(String name) {
        this.nameField.setText(name);
    }

    public void setDefault(boolean b) {
        defaultRadio.setSelected(b);
    }

    public boolean isSelected() {
        return selectedCheck.isSelected();
    }

    public Sequence getSequence() {
        return sequence;
    }
    public String getSequenceName() {
        return nameField.getText();
    }
    public boolean isSequenceDefault() {
        return defaultRadio.isSelected();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        editButton = new javax.swing.JButton();
        nameField = new javax.swing.JTextField();
        defaultRadio = new javax.swing.JRadioButton();
        selectedCheck = new javax.swing.JCheckBox();

        editButton.setText("Edit Sequence");

        defaultRadio.setText("Default");

        selectedCheck.setText(" ");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(selectedCheck)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(nameField, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(editButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(defaultRadio))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(selectedCheck)
                .addComponent(nameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(editButton)
                .addComponent(defaultRadio))
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JRadioButton defaultRadio;
    private javax.swing.JButton editButton;
    private javax.swing.JTextField nameField;
    private javax.swing.JCheckBox selectedCheck;
    // End of variables declaration//GEN-END:variables

    class EditListener implements ActionListener {
        private SequenceFrame lastSeq = null;
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == editButton) {
                showEditor();
            }
        }

        private synchronized void showEditor() {
            if (lastSeq == null || !lastSeq.isVisible()) {
                SequenceFrame sf = new SequenceFrame(sequence);
                sf.setLocationRelativeTo(null);
                sf.setVisible(true);
                lastSeq = sf;
            }
        }
    }
}
